import flv from 'flv.js'
var u = navigator.userAgent, app = navigator.appVersion, ua = navigator.userAgent.toLowerCase()
export	const isMobile = (function(){
  let flag = u.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
  return flag
})()
export	let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);               //ios终端
export	let isWeixin = ua.match(/MicroMessenger/i) == "micromessenger";       //是否微信  
export	let isqq = u.match(/\sQQ/i) == " QQ";
export	let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
export  let isHuwei = navigator.userAgent.toLowerCase().indexOf('huawei') > -1 || navigator.userAgent.toLowerCase().indexOf('honor') > -1
export function createNode(htmlStr) {
	var div = document.createElement("div");
	div.innerHTML = htmlStr;
	return div.childNodes[0];
}
export function	stringLen(str){
	return str.replace(/[^\x00-\xff]/g, 'xx').length;
}
export let flvCommon = (function(){
	let flvSupport = (function(){
		let flvSuppot
		// if(isHuwei && (!isqq || !isWeixin)){
		// 	flvSuppot = false
		// }else{
		flvSuppot = flv.getFeatureList().mseLiveFlvPlayback && !isIOS
		// }
		return flvSuppot;
	})()
	let pcflvSupport = (function(){
		let flvSuppot
		flvSuppot = flv.getFeatureList().mseLiveFlvPlayback && !isIOS
		return flvSuppot;
	})()
	let createFlvPlayer = function(url,videoElement){
    let flvPlayer = flv.createPlayer({
      type: 'flv',
      url: url,
			isLive: true
		},{
			enableWorker: true,
			enableStashBuffer: false,
			stashInitialSize: 128,
			deferLoadAfterSourceOpen: true,
			isLive: true
		});
    flvPlayer.attachMediaElement(videoElement);
    return flvPlayer;
	}
	let destroyFlvPlayer = function(flvPlayer,flvPlayerPlaying){
		if(flvPlayer){
			flvPlayer.pause();
			flvPlayer.unload();
			flvPlayer.detachMediaElement();
			flvPlayer.destroy();
		}
	}
	return{
		flvSupport:flvSupport,
		pcflvSupport:pcflvSupport,
		createFlvPlayer:createFlvPlayer,
		destroyFlvPlayer:destroyFlvPlayer,
	}
})()
export function hasClass (el, className) {
  if (el.classList) {
    return Array.prototype.some.call(el.classList, item => item === className)
  } else {
    return !!el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'))
  }
}
export function addClass (el, className) {
  if (el.classList) {
    className.replace(/(^\s+|\s+$)/g, '').split(/\s+/g).forEach(item => {
      item && el.classList.add(item)
    })
  } else if (!hasClass(el, className)) {
    el.className += ' ' + className
  }
}
export function removeClass (el, className) {
  if (el.classList) {
    className.split(/\s+/g).forEach(item => {
      el.classList.remove(item)
    })
  } else if (hasClass(el, className)) {
    className.split(/\s+/g).forEach(item => {
      let reg = new RegExp('(\\s|^)' + item + '(\\s|$)')
      el.className = el.className.replace(reg, ' ')
    })
  }
}
export function toggleClass (el, className) {
  className.split(/\s+/g).forEach(item => {
    if (hasClass(el, item)) {
      removeClass(el, item)
    } else {
      addClass(el, item)
    }
  })
}
export function arrayInsert(arr,index, item){
	arr.splice(index, 0, item)
}
export function arrayRemove(arr,from, to){
	var rest = arr.slice((to || from) + 1 || arr.length);
	arr.length = from < 0 ? arr.length + from : from;
	arr.push.apply(arr, rest);
}
export function sleep(duration) {
	return new Promise(function(resolve, reject) {
		setTimeout(resolve,duration);
	})
}